package com.example.cggame;

import android.os.Handler;
import android.content.Context;
import android.util.AttributeSet;
import android.view.*;

public class GameView extends SurfaceView  implements SurfaceHolder.Callback
{
	private GameThread thread;

	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);

		//So we can listen for events...
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		setFocusable(true); 

		//and instantiate the thread
		thread = new GameThread(holder, context, new Handler());
	}  

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		return thread.getGameState().keyPressed(keyCode, msg);
	}

	//Implemented as part of the SurfaceHolder.Callback interface
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		//Mandatory, just swallowing it for this example

	}

	//Implemented as part of the SurfaceHolder.Callback interface
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.start();
	}

	//Implemented as part of the SurfaceHolder.Callback interface
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		thread.stop();
	}
}